package com.fozu.office.sale.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fozu.common.config.Constants;
import com.fozu.common.pojo.ResultVO;
import com.fozu.common.util.IdGen;
import com.fozu.office.sale.entity.AchievementVO;
import com.fozu.office.sale.service.AchievementServiceImpl;
import com.fozu.office.system.entity.UserVO;

/*
 * 销售绩效
 */
@Controller
@RequestMapping(value="achievement")
public class AchievementController {
	@Autowired
	private AchievementServiceImpl achievementService;
	
	//跳转到绩效列表页面
	@RequestMapping(value="achievementList.shtml")
	public String toAchievementList(){
		return "sale/achievement/mentList";
	}
	//跳转到添加页面
	@RequestMapping(value="mentAdd.shtml")
	public String toAchievementAdd(){
		return "sale/achievement/mentAdd";
	}
	//跳转到查看页面
	@RequestMapping(value="mentQuery.shtml")
	public String toAchievementQuery(){
		return "sale/achievement/mentQuery";
	}
	//跳转到修改页面
	@RequestMapping(value="edit.shtml")
	public String toAchievementUpdate(){
		return "sale/achievement/mentUpdate";
	}
	
	
	//绩效列表（分页）
	@PostMapping(value="getAchievementList")
	@ResponseBody
	public ResultVO getAchievementList(
										@RequestParam(value="page",required=true) Integer page,
										@RequestParam(value="size",required=true) Integer size,
										@RequestParam(value="userId",required=false) String userId){
		try{
			AchievementVO achievement = new AchievementVO();
			achievement.setUserId(userId);
			IPage<AchievementVO> achievementList = achievementService.listAchievement(new Page<AchievementVO>(page,size), achievement);
			return new ResultVO(achievementList);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"获取失败",null);
		}
	}
	//添加
	@PostMapping(value="addMent")
	@ResponseBody
	public ResultVO addMent(HttpServletRequest request,
							@RequestParam(value="userId",required=true)String userId,
							@RequestParam(value="level",required=true)Integer level,
							@RequestParam(value="remarks",required=true)String remarks){
		try{
			String id = IdGen.getId();
			AchievementVO ment = new AchievementVO(id,userId,level,remarks);
			UserVO loginUser=(UserVO) request.getSession().getAttribute("loginUser");
			ment.setCreateBy(loginUser.getId());
			ment.setCreateDate(new Date());
			ment.setUpdateBy(loginUser.getId());
			ment.setUpdateDate(new Date());
			achievementService.addMent(ment);
			return new ResultVO(Constants.status_SUCCESS,"添加成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_Exception,"系统异常",null);
		}
	}
	//根据id删除user(批量+单个)
	@PostMapping(value="deleteMent")
	@ResponseBody
	public ResultVO deleteMents(@RequestParam(value="ids",required=true) String ids){
		try{
			String [] mentIds = ids.split(",");
			achievementService.delMentByIds(mentIds);
			return new ResultVO(Constants.status_SUCCESS,"删除成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"删除失败",null);
		}
	}
	//根据id那对象
	@PostMapping(value="getMentById")
	@ResponseBody
	public ResultVO getMentById(@RequestParam(value="mentId") String mentId){
		try{
			AchievementVO ment=achievementService.getMentById(mentId);
			return new ResultVO(ment);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"获取失败",null);
		}
	}
	//修改
	@PostMapping(value="updateMent")
	@ResponseBody
	public ResultVO updateMent(HttpServletRequest request,
							   @RequestParam(value="id",required=true)String id,
							   @RequestParam(value="userId",required=true)String userId,
							   @RequestParam(value="level",required=true) Integer level,
							   @RequestParam(value="remarks",required=false)String remarks){
		try{
			UserVO loginUser = (UserVO) request.getSession().getAttribute("loginUser");
			AchievementVO ment = new AchievementVO(id,userId,level,remarks);
			ment.setUpdateBy(loginUser.getId());
			ment.setUpdateDate(new Date());
			achievementService.updateMent(ment);
			return new ResultVO(Constants.status_SUCCESS,"修改成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"修改失败",null);
		}
	}
}
